package org.jeecg.modules.paycenter.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.CheckBankCardIUtil;
import org.jeecg.modules.paycenter.channel.zxbank.service.ZXBankService;
import org.jeecg.modules.paycenter.enums.BillTypeEnum;
import org.jeecg.modules.paycenter.service.BillService;
import org.jeecg.modules.paycenter.service.IPayChannelService;
import org.jeecg.modules.paycenter.service.PayChannelFactory;
import org.jeecg.modules.paycenter.service.WalletService;
import org.jeecg.modules.wlhy.bill.entity.HyBill;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBill;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyBillService;
import org.jeecg.modules.wlhy.bill.service.IHyCustomerBillService;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 支付服务
 */
@RestController
@RequestMapping("/shipping/hyShippingNote")
public class HyPayController {

    @Autowired private IHyShippingNoteService hyShippingNoteService;
    @Autowired private IHyCustomerService hyCustomerService;
    @Autowired private IHyShippingNoteBillService hyShippingNoteBillService;
    @Autowired private IHySettingService settingService;
    @Autowired private BillService billService;
    @Autowired private UserUtil userUtil;
    @Autowired private WalletService walletService;
    @Autowired private ZXBankService zxBankService;
    @Autowired private IHyBillService hyBillService;
    @Autowired private IHyCustomerBillService hyCustomerBillService;

    @Value("${pay-config.trade.begin:8}")
    private String TRADE_BEGIN;

    @Value("${pay-config.trade.end:19}")
    private String TRADE_END;

    //region 支付

    @AutoLog(value = "运单表-货主批量支付")
    @ApiOperation(value="运单表-货主批量支付", notes="运单表-货主批量支付")
    @PostMapping(value = "/payBatch")
    @RequiresPermissions("shipping:pay")
    @Transactional
    public Result<?> payBatch(@RequestBody JSONObject jo) {

        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }

        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if (!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }

        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }

        String pw = jo.getString("pw");
        if (StringUtils.isEmpty(pw)) {
            return Result.error("支付密码不能为空");
        }
        if (!walletService.checkPwd(pw)) {
            return Result.error("支付密码错误");
        }

        String payChannel = jo.getString("payChannel");
        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if (!channelService.checkChannel()) {
            return Result.error("未开通支付服务");
        }
        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        if(StringUtils.isEmpty(customer.getCustomerShoreName())){
            return Result.error("货主未设置简称，请先设置简称");
        }
        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if (hyBills == null || hyBills.size() == 0) {
            return Result.error("请选择运单");
        }
        Result<?> ckinfo = billService.checkBills(hyBills, customer.getId());
        if (!ckinfo.isSuccess()) {
            return ckinfo;
        }
        return channelService.billPayBatch(hyBills, customer);
    }

    @AutoLog(value = "经纪人批量代支付")
    @ApiOperation(value="经纪人批量代支付", notes="经纪人批量代支付")
    @PostMapping(value = "/agentPayBatch")
    @RequiresPermissions("shipping:pay")
    @Transactional
    public Result<?> agentPayBatch(@RequestBody JSONObject jo) {

        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }


        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if (!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }
        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }

        String pw = jo.getString("pw");
        if (StringUtils.isEmpty(pw)) {
            return Result.error("支付密码不能为空");
        }
        if (!walletService.checkPwd(pw)) {
            return Result.error("支付密码错误");
        }

        String payChannel = jo.getString("payChannel");
        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if (!channelService.checkChannel()) {
            return Result.error("未开通支付服务");
        }

        HyCustomer agent = userUtil.getCustomer();
        if (agent == null) {
            return Result.error("经纪人信息不存在");
        }
        if(StringUtils.isEmpty(agent.getCustomerShoreName())){
            return Result.error("经纪人未设置简称，请先设置简称");
        }
        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if (hyBills == null || hyBills.size() == 0) {
            return Result.error("请选择运单");
        }
        Result<?> ckinfo = billService.checkAgentBills(hyBills, agent.getId());
        if (!ckinfo.isSuccess()) {
            return ckinfo;
        }
        String customerId = (String) ckinfo.getResult();
        HyCustomer customer = hyCustomerService.getById(customerId);
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        if(StringUtils.isEmpty(customer.getCustomerShoreName())){
            return Result.error("货主未设置简称，请先设置简称");
        }
        return channelService.billPayBatch(hyBills, customer);
    }

    @AutoLog(value = "经纪人批量垫付运费")
    @ApiOperation(value="经纪人批量垫付运费", notes="经纪人批量垫付运费")
    @PostMapping(value = "/agentDianFuPayBatch")
    @RequiresPermissions("shipping:pay")
    @Transactional
    public Result<?> agentDianFuPayBatch(@RequestBody JSONObject jo) {

        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }


        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if (!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }
        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }
        String pw = jo.getString("pw");
        if (StringUtils.isEmpty(pw)) {
            return Result.error("支付密码不能为空");
        }
        if (!walletService.checkPwd(pw)) {
            return Result.error("支付密码错误");
        }
        String payChannel = jo.getString("payChannel");
        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if (!channelService.checkChannel()) {
            return Result.error("未开通支付服务");
        }
        HyCustomer agent = userUtil.getCustomer();
        if (agent == null) {
            return Result.error("经纪人信息不存在");
        }
        if(StringUtils.isEmpty(agent.getCustomerShoreName())){
            return Result.error("经纪人未设置简称，请先设置简称");
        }
        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if(hyBills == null || hyBills.size()==0){
            return Result.error("请选择运单");
        }
        Result<?> ckinfo = billService.checkAgentBills(hyBills, agent.getId());
        if (!ckinfo.isSuccess()) {
            return ckinfo;
        }
        String customerId = (String) ckinfo.getResult();
        HyCustomer customer = hyCustomerService.getById(customerId);
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        if(StringUtils.isEmpty(customer.getCustomerShoreName())){
            return Result.error("货主未设置简称，请先设置简称");
        }
        return channelService.agentDianFuPayBatch(BillTypeEnum.PAY, hyBills, agent, customer);
    }

    @AutoLog(value = "运单表-经纪人批量重新支付运费")
    @ApiOperation(value="运单表-经纪人批量重新支付运费", notes="运单表-经纪人批量重新支付运费")
    @PostMapping(value = "/agentRePayBatch")
    @Transactional
    public Result<?> agentRePayBatch(@RequestBody JSONObject jo) {

        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }


        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if(!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }

        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }

        String pwd = jo.getString("pwd");
        if (StringUtils.isEmpty(pwd)) {
            return Result.error("支付密码不能为空");
        }
        if (!walletService.checkPwd(pwd)) {
            return Result.error("支付密码错误");
        }

        HyCustomer customer = userUtil.getCustomer();
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if(hyBills == null || hyBills.size()==0){
            return Result.error("请选择运单");
        }
        String payChannel = hyBills.get(0).getPayChannel();
        for (HyBill hyBill : hyBills) {
            if(!StringUtils.equals(payChannel , hyBill.getPayChannel())){
                return Result.error("选择的运单包含不同的支付渠道,不能支付!");
            }
            if(!"7".equals(hyBill.getPayStatus())){
                return Result.error("不是支付失败运单，不能重新支付!");
            }
            if(!StringUtils.equals(customer.getId(),hyBill.getAgentId())){
                return Result.error("运单信息错误!");
            }
        }
        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if(!channelService.checkChannel()){
            return Result.error("未开通支付服务");
        }
        return channelService.billRePayBatch(hyBills,customer);
    }

    @AutoLog(value = "运单表-系统批量支付")
    @ApiOperation(value="运单表-系统批量支付", notes="运单表-系统批量支付")
    @PostMapping(value = "/syspayBatch")
    @RequiresPermissions("shipping:batchSysPay")
    @Transactional
    public Result<?> sysPayBatch(@RequestBody JSONObject jo) {

        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }


        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if (!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }

        String payChannel = jo.getString("payChannel");
        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if (!channelService.checkChannel()) {
            return Result.error("未开通支付服务");
        }

        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }
        String pw = jo.getString("pw");
        if (StringUtils.isEmpty(pw)) {
            return Result.error("支付密码不能为空");
        }
        //检查支付密码
        if (!walletService.checkPwd(pw)) {
            return Result.error("支付密码错误");
        }

        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if (hyBills == null || hyBills.size() == 0) {
            return Result.error("请选择运单");
        }
        Result<?> ckinfo = billService.checkBills(hyBills);
        if (!ckinfo.isSuccess()) {
            return ckinfo;
        }
        String customerId = (String) ckinfo.getResult();
        HyCustomer customer = hyCustomerService.getById(customerId);
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        if(StringUtils.isEmpty(customer.getCustomerShoreName())){
            return Result.error("货主未设置简称，请先设置简称");
        }
        return channelService.billPayBatch(hyBills, customer);
    }

    @AutoLog(value = "运单表-系统批量平台垫付运费")
    @ApiOperation(value="运单表-系统批量平台垫付运费", notes="运单表-系统批量平台垫付运费")
    @PostMapping(value = "/sysPlateformPayBatch")
    @RequiresPermissions("shipping:batchPlateformPay")
    @Transactional
    public Result<?> sysPlateformPayBatch(@RequestBody JSONObject jo) {


        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }


        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if(!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }

        String payChannel = jo.getString("payChannel");
        if("5".equals(payChannel)){
            return Result.error("不支持线下支付");
        }

        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if(!channelService.checkChannel()){
            return Result.error("未开通支付服务");
        }

        String pwd = jo.getString("pw");
        if (StringUtils.isEmpty(pwd)) {
            return Result.error("支付密码不能为空");
        }
        //检查管理员支付密码
        if(!walletService.checkPwd(pwd)){
            return Result.error("支付密码错误");
        }

        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }
        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if (hyBills == null || hyBills.size() == 0) {
            return Result.error("请选择运单");
        }
        Result<?> ckinfo = billService.checkBills(hyBills);
        if (!ckinfo.isSuccess()) {
            return ckinfo;
        }
        String customerId = (String) ckinfo.getResult();
        HyCustomer customer = hyCustomerService.getById(customerId);
        if (customer == null) {
            return Result.error("货主信息不存在");
        }
        if(StringUtils.isEmpty(customer.getCustomerShoreName())){
            return Result.error("货主未设置简称，请先设置简称");
        }
        return channelService.billPlateformPay(hyBills, customer);
    }

    @AutoLog(value = "运单表-货主批量重新支付运费")
    @ApiOperation(value="运单表-货主批量重新支付运费", notes="运单表-货主批量重新支付运费")
    @PostMapping(value = "/rePayBatch")
    @Transactional
    public Result<?> rePayBatch(@RequestBody JSONObject jo) {


        if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
            return Result.error("不在交易时间内, 详情请咨询系统管理员！");
        }

        String settingValue = settingService.getSettingValueByTag("bank_relative");
        if(!"1".equals(settingValue)) {
            return Result.error("系统暂时关闭支付功能！");
        }

        String ids = jo.getString("ids");
        if (StringUtils.isEmpty(ids)) {
            return Result.error("运单不能为空");
        }

        String pwd = jo.getString("pwd");
        if (StringUtils.isEmpty(pwd)) {
            return Result.error("支付密码不能为空");
        }
        //检查支付密码
        if(!walletService.checkPwd(pwd)){
            return Result.error("支付密码错误");
        }

        List<HyBill> hyBills = hyBillService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
        if(hyBills == null || hyBills.size()==0){
            return Result.error("请选择运单");
        }
        String payChannel = hyBills.get(0).getPayChannel();
        for (HyBill hyBill : hyBills) {
            if(!StringUtils.equals(payChannel , hyBill.getPayChannel())){
                return Result.error("选择的运单包含不同的支付渠道,不能支付!");
            }
            if(!"7".equals(hyBill.getPayStatus())){
                return Result.error("不是支付失败运单，不能重新支付!");
            }
        }
        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if(!channelService.checkChannel()){
            return Result.error("未开通支付服务");
        }

        HyCustomer customer = null;
        if(userUtil.isSys()){
            String customerId = billService.getHyBillCustomerId(hyBills);
            customer = hyCustomerService.getById(customerId);
            if (customer == null) {
                return Result.error("货主信息不存在");
            }
        }else {
            customer = userUtil.getCustomer();
            if (customer == null) {
                return Result.error("货主信息不存在");
            }
            String customerId = customer.getId();
            billService.checkHyBillCustomerId(hyBills , customerId);
        }
        return channelService.billRePayBatch(hyBills,customer);
    }

    //endregion



    //region 线下支付

    @ApiOperation(value="查询运单支付凭证信息", notes="查询运单支付凭证信息")
    @GetMapping(value = "/getBillInfo")
    public Result<?> getBillInfo(@RequestParam(name="id",required=true) String id) {
        HyShippingNote hyShippingNote = hyShippingNoteService.getById(id);
        if(hyShippingNote==null){
            return Result.error("未找到运单数据");
        }
        if(!"5".equals(hyShippingNote.getPayChannel())){
            return Result.error("支付渠道错误");
        }
        HyShippingNoteBill bill = hyShippingNoteBillService.getOne(new QueryWrapper<HyShippingNoteBill>()
                .eq("shipping_id", id)
                .eq("bill_type","1")
        );
        if(bill== null){
            return Result.error("数据错误");
        }
        return Result.OK(bill);
    }

    @PutMapping(value = "/setBillInfo")
    @Transactional
    @RequiresPermissions("wallet:recharge")
    public Result<?> setBill(@RequestBody Map<String,String> map){
        String id = map.get("id");
        if(StringUtils.isEmpty(id)){
            return Result.error("id参数错误");
        }

        String craccname = map.get("craccname");
        if(StringUtils.isEmpty(craccname)){
            return Result.error("司机收款账户名不能为空");
        }
        String craccno = map.get("craccno");
        if(StringUtils.isEmpty(craccno)){
            return Result.error("司机收款账户号不能为空");
        }
        String bankCode = map.get("bankCode");
        if(StringUtils.isEmpty(bankCode)){
            return Result.error("银行代码不能为空");
        }

        //Added at 2023-10-02 通过交通部代码集判断银行代码是否正确
        if (StrUtil.isEmpty(CheckBankCardIUtil.getBankNameByBankCode(bankCode))) {
            return Result.error("银行代码错误");
        }

        String bankHdImg = map.get("bankHdImg");
//        if(StringUtils.isEmpty(bankHdImg)){
//            return Result.error("支付凭证不能为空");
//        }

        HyShippingNote hyShippingNote = hyShippingNoteService.getById(id);
        if(hyShippingNote==null){
            return Result.error("未找到运单数据");
        }

        HyShippingNoteBill shippingNoteBill = hyShippingNoteBillService.getOne(new QueryWrapper<HyShippingNoteBill>().eq("shipping_id", id));
        if(shippingNoteBill==null){
            return Result.error("数据不存在");
        }
        shippingNoteBill.setCraccname(craccname);
        shippingNoteBill.setCraccno(craccno);
        shippingNoteBill.setBankCode(bankCode);
        shippingNoteBill.setBankHdImg(bankHdImg);
        hyShippingNoteBillService.updateById(shippingNoteBill);

        hyShippingNote.setShippingPayStatus("6");
        hyShippingNoteService.updateById(hyShippingNote);
        return Result.OK("操作成功");
    }
    @PutMapping(value = "/setBillInfo1")
    @Transactional
    @RequiresPermissions("wallet:recharge")
    public Result<?> setBill1(@RequestBody Map<String,String> map){
        String id = map.get("id");
        if(StringUtils.isEmpty(id)){
            return Result.error("id参数错误");
        }

        String craccname = map.get("craccname");
        if(StringUtils.isEmpty(craccname)){
            return Result.error("司机收款账户名不能为空");
        }
        String craccno = map.get("craccno");
        if(StringUtils.isEmpty(craccno)){
            return Result.error("司机收款账户号不能为空");
        }
        String bankCode = map.get("bankCode");
        if(StringUtils.isEmpty(bankCode)){
            return Result.error("银行代码不能为空");
        }

        //Added at 2023-10-02 通过交通部代码集判断银行代码是否正确
        if (StrUtil.isEmpty(CheckBankCardIUtil.getBankNameByBankCode(bankCode))) {
            return Result.error("银行代码错误");
        }

        String bankHdImg = map.get("bankHdImg");
//        if(StringUtils.isEmpty(bankHdImg)){
//            return Result.error("支付凭证不能为空");
//        }

        HyShippingNote hyShippingNote = hyShippingNoteService.getById(id);
        if(hyShippingNote==null){
            return Result.error("未找到运单数据");
        }

        HyShippingNoteBill shippingNoteBill = hyShippingNoteBillService.getOne(new QueryWrapper<HyShippingNoteBill>().eq("shipping_id", id).eq("bill_type","1"));
        if(shippingNoteBill==null){
            return Result.error("数据不存在");
        }

        shippingNoteBill.setCraccname(craccname);
        shippingNoteBill.setCraccno(craccno);
        shippingNoteBill.setBankCode(bankCode);
        shippingNoteBill.setBankHdImg(bankHdImg);
        hyShippingNoteBillService.updateById(shippingNoteBill);

        hyShippingNote.setShippingPayStatus("6");
        hyShippingNoteService.updateById(hyShippingNote);
        return Result.OK("操作成功");
    }

    //endregion

    //region 电子回单
    @PostMapping(value = "/downloadZxHD")
    public Result<?> downloadZxHD(@RequestBody JSONObject jo) {
        String ids = jo.getString("ids");
        if(StringUtils.isEmpty(ids)){
            return Result.error("ids不能为空");
        }
        HyConfig config = HyConfigUtils.getConfig();
        if (!config.getZxIzUse()) {
            return Result.error("未开通中信银行支付服务！");
        }

        List<HyShippingNoteBill> hyShippingNoteBills = hyShippingNoteBillService.listByIds(Arrays.asList(ids.split(",")));
        if(hyShippingNoteBills.size() == 0){
            return Result.error("未找到数据");
        }
        Date pdate = null;
        for (HyShippingNoteBill hyShippingNoteBill : hyShippingNoteBills) {
            if(!StringUtils.equals(ZXBankService.CHANNEL,hyShippingNoteBill.getPayChannel())){
                return Result.error("支付渠道错误");
            }

            if(!"0".equals(hyShippingNoteBill.getRespSource())){
                return Result.error("包含未支付完成数据");
            }
            Date payDt = hyShippingNoteBill.getPayDt();
            if(payDt == null){
                payDt = hyShippingNoteBill.getCreateTime();
            }

            if(pdate == null){
                pdate = payDt;
            }else {
                if(!DateUtil.isSameDay(pdate,payDt)){
                    return Result.error("所选数据支付完成日期需要是同一天,请重新选择");
                }
            }
            if(!StringUtils.isEmpty(hyShippingNoteBill.getBankHd())){
                return Result.error("所选数据包含已下载完成电子回单数据,请重新选择");
            }
        }
        return zxBankService.downloadZxHd(hyShippingNoteBills,pdate);
    }

    @ApiOperation(value="批量下载CustomerBill电子回单", notes="批量下载CustomerBill电子回单")
    @RequestMapping(value = "/batchDownloadCustomerBillHd")
    public Result<?> batchDownloadCustomerBillHd(@RequestParam(name="ids")String ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error("id不能为空");
        }

        List<HyCustomerBill> customerBills = hyCustomerBillService.listByIds(Arrays.asList(ids.split(",")));
        if (customerBills.size() == 0) {
            return Result.error("数据不存在");
        }
        String payChannel = customerBills.get(0).getPayChannel();
        for (HyCustomerBill customerBill : customerBills) {
            if (!StringUtils.equals(payChannel, customerBill.getPayChannel())) {
                return Result.error("只能选择一种支付渠道");
            }
            if (!"0".equals(customerBill.getRespSource())) {
                return Result.error("所选数据包含未支付完成的数据,请重新选择");
            }
        }

        boolean isSys = userUtil.isSys();
        String customerId = null;
        if (isSys) {
            customerId = customerBills.get(0).getCustomerId();
        } else {
            HyCustomer customer = userUtil.getCustomer();
            if (customer == null) {
                return Result.error("货主信息不存在");
            }
            customerId = customer.getId();
        }
        for (HyCustomerBill customerBill : customerBills) {
            if (!StringUtils.equals(customerId, customerBill.getCustomerId())) {
                return Result.error("所选数据不是同一个货主,不能下载");
            }
        }

        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if (!channelService.checkChannel()) {
            return Result.error("未开通支付服务");
        }

        //检查未下载电子回单
        List<HyCustomerBill> needDownloadBill = new ArrayList<>();
        for (HyCustomerBill customerBill : customerBills) {
            if (StringUtils.isEmpty(customerBill.getBankHdFu())) {
                needDownloadBill.add(customerBill);
            }
        }
        //下载电子回单
        if (needDownloadBill.size() > 0) {
            return channelService.batchDownloadCustomerBilHd(needDownloadBill);
        }else {
            return Result.error("无需要下载的数据");
        }
    }


    @ApiOperation(value="批量下载ShippingBill电子回单", notes="批量下载ShippingBill电子回单")
    @RequestMapping(value = "/batchDownloadShippingBillHd")
    public Result<?> batchDownloadShippingBillHd(@RequestParam(name="ids")String ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error(500,"id不能为空");
        }
        if (!userUtil.isSys()) {
            return Result.error(500,"没有操作权限");
        }

        List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.listByIds(Arrays.asList(ids.split(",")));
        if (shippingNoteBills.size() == 0) {
            return Result.error(500,"数据不存在");
        }
        String payChannel = shippingNoteBills.get(0).getPayChannel();
        for (HyShippingNoteBill bill : shippingNoteBills) {
            if (!StringUtils.equals(payChannel, bill.getPayChannel())) {
                return Result.error(500,"只能选择一种支付渠道");
            }
            if (!"0".equals(bill.getRespSource())) {
                return Result.error(500,"所选数据包含未支付完成的数据,请重新选择");
            }
        }

        //检查支付渠道
        IPayChannelService channelService = PayChannelFactory.getPayChannel(payChannel);
        if (!channelService.checkChannel()) {
            return Result.error(500,"未开通支付服务");
        }

        //检查未下载电子回单
        List<HyShippingNoteBill> needDownloadBill = new ArrayList<>();
        for (HyShippingNoteBill bill : shippingNoteBills) {
            if (StringUtils.isEmpty(bill.getBankHd())) {
                needDownloadBill.add(bill);
            }
        }
        //下载电子回单
        if (needDownloadBill.size() > 0) {
            return channelService.batchDownloadShippingBillHd(needDownloadBill);
        }else {
            return Result.error(500,"无需要下载的数据");
        }
    }
    //endregion
}
